package com.acxy.ants.core.plugin.entity;

import com.acxy.ants.common.kit.StrEncryptKit;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-09-07
 */
public class SqlParams {

    //数据表名称
    private String unique;

    //操作类型
    private Symbol symbol;

    //拼装后的预处理SQL语句
    private String sql;

    //条件参数
    private Object[] params;

    //存放条件集合
    private List<String> conditions;

    //存放数据值
    private List<Object> values;

    //存放select label 标签
    private String labelStr = "";

    //存放条件
    private String whereStr = "";

    //常用左链接关联查询字符串
    private String leftJoinStr = "";

    //存放排序字符串
    private String orderByStr = "";

    //存放分页字符串
    private String limitStr = "";

    public SqlParams(){};

    public SqlParams(String sql, Object... params){
        this.sql = sql;
        this.params = params;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public Object[] getParams() {
        return params;
    }
    public void setParams(Object[] params) {
        this.params = params;
    }

    public String getConditions() {
        //如果是insert操作直接返回无条件
        if(symbol == Symbol.INSERT) return "";
        StringBuffer sb = new StringBuffer();
        if (conditions != null && conditions.size() != 0) {
            sb.append(" where ");
            if(params == null) {
                params = new Object[conditions.size()];
                List<Object> values = getValues();
                for (int i = 0; i < conditions.size(); i++) {
                    sb.append(conditions.get(i));
                    params[i] = values.get(i);
                }
            }else {
                int len = params.length;
                params = Arrays.copyOf(params, len + values.size());
                List<Object> values = getValues();
                for (int i = 0; i < conditions.size(); i++) {
                    sb.append(conditions.get(i));
                    params[len + i] = values.get(i);
                }
            }
        }
        return sb.toString().replace(" where and", " where");
    }


    public List<Object> getValues() {
        return values;
    }

    public String getOrderByStr() {
        return orderByStr;
    }

    public void setOrderByStr(String orderByStr) {
        this.orderByStr = orderByStr;
    }

    public String getLimitStr() {
        return limitStr;
    }

    public void setLimitStr(String limitStr) {
        this.limitStr = limitStr;
    }

    public void addCondition(String condition) {
        if (conditions == null) conditions = new ArrayList<>();
        conditions.add(condition);
    }

    public void addValue(Object value) {
        if (values == null) values = new ArrayList<>();
        values.add(value);
    }


    public String getLeftJoinStr() {
        return leftJoinStr;
    }

    public void setLeftJoinStr(String leftJoinStr) {
        this.leftJoinStr = leftJoinStr;
    }

    public String getLabelStr() {
        return labelStr;
    }

    public void setLabelStr(String labelStr) {
        this.labelStr = labelStr;
    }

    public String getWhereStr() {
        return whereStr;
    }

    public void setWhereStr(String whereStr) {
        this.whereStr = whereStr;
    }

    public void setSymbol(Symbol symbol) {
        this.symbol = symbol;
    }

    public Symbol getSymbol() {
        return symbol;
    }

    public String getUnique() {
        return unique;
    }

    public void setUnique(String unique) {
        this.unique = unique;
    }

    public String getCacheKey(){
        String key = unique + (params==null?"":JSON.toJSONString(params))+JSON.toJSONString(values);
        return StrEncryptKit.MD5(key);
    }
}
